#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <map>
#include <unordered_map>
#include <vector>
#include <set>
#include <queue>

using namespace std;
using LL = long long;
using PII = pair<int, int>;
const int N = 4e6 + 10;

struct Seg{
    LL x, y, eq;
}seg[N];

int sz;
int n;
int p[N];
vector<LL> nums;

int f(LL x){
    int t = lower_bound(nums.begin(), nums.end(), x) - nums.begin();
    t ++;
    return t;
}

int find(int x){
    if(x != p[x]){
        p[x] = find(p[x]);
    }
    return p[x];
}

void merge(int x, int y){
    int px = find(x), py = find(y);
    p[px] = py;
}

void solve(){
    nums.clear();

    cin >> n;
    for(int i = 1; i <= n; i ++) {
        cin >> seg[i].x >> seg[i].y >> seg[i].eq;
        nums.push_back(seg[i].x);
        nums.push_back(seg[i].y);
    }

    sort(nums.begin(), nums.end());
    nums.erase(unique(nums.begin(), nums.end()), nums.end());
    sz = nums.size();

    for(int i = 1; i <= sz * 2; i ++) p[i] = i;


    bool ok = true;
    for(int i = 1; i <= n; i ++){
        int x = f(seg[i].x), y = f(seg[i].y), eq = seg[i].eq;
        if(eq == 1){
            int px = find(x), py = find(y);
            if(px != py) p[px] = py;
        }
    }

    for(int i = 1; i <= n; i ++){
        int x = f(seg[i].x), y = f(seg[i].y), eq = seg[i].eq;
        if(eq == 0){
            int px = find(x), py = find(y);
            if(px == py) {
                ok = false;
                break;
            }
        }
    }

    if(ok) cout << "YES" << '\n';
    else cout << "NO" << '\n';
}

int main(){
#ifdef LOCALFLAG
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
#endif


    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int T;
    cin >> T;
    while(T--){
        solve();
    }

    return 0;
}